<template>
  <div>
    <div class="provider-list">
      <div class="content">
        <div class="project-info">
          <div class="form-left-right">
            <el-form
              style="max-width: 1200px"
              :model="state.form"
              :rules="state.rules"
              ref="formRef"
              label-position="top"
            >
              <el-form-item label="标题：" prop="biaoti">
                <el-col :span="10">
                  <el-input size="large" v-model="state.form.biaoti" />
                </el-col>
              </el-form-item>
              <el-form-item label="类型：" prop="leixing">
                <el-select
                  v-model="state.form.leixing"
                  size="large"
                  placeholder=""
                  style="width: 240px"
                >
                  <el-option
                    v-for="item in state.type"
                    :key="item.dicValue"
                    :label="item.dicLabel"
                    :value="item.dicValue"
                  />
                </el-select>
              </el-form-item>
              <el-form-item label="选择文件：">
                <bjz-oss
                  :previewList="state.form.wenjian"
                  :accept="['.jpg', '.png', '.jpeg']"
                  @success="successImg"
                  @remove="removeImg"
                />
              </el-form-item>
              <el-form-item class="vue-editor" label="资讯内容：">
                <editor :initValue="state.form.baogaoneirong" @onVal="onVal" />
              </el-form-item>
              <el-form-item label="状态：">
                <el-radio-group v-model="state.form.shangjia">
                  <el-radio :label="1">上架</el-radio>
                  <el-radio :label="0">下架</el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item class="button-group">
                <el-button
                  style="margin-left: 100px"
                  type="primary"
                  @click="submitForm(formRef)"
                  >提交</el-button
                >
                <el-button
                  type="primary"
                  style="margin-left: 50px"
                  @click="$router.go(-1)"
                  >取消</el-button
                >
              </el-form-item>
            </el-form>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script setup>
import { reactive, ref } from "vue";
import { useRoute, useRouter } from "vue-router";
import { GetReport, AddReport, EditReport } from "@/api/report";
import { GetDictionaryTree } from "@/api/system";
import { baseMsg } from "@/utils/utils";
const route = useRoute();
const router = useRouter();
const formRef = ref(null)
const state = reactive({
  form: {
    biaoti: "",
    leixing: "",
    wenjian: [],
    shangjia: 1,
    // 富文本
    baogaoneirong: "",
    shangjia: 0,
  },
  rules: {
    biaoti: [{ required: true, message: "请输入标题名称", trigger: "blur" }],
    leixing: [{ required: true, message: "请选择类型", trigger: "change" }],
  },
  type: [],
});
// 根据id查询内容
const GetInfo = () => {
  if (route.query.type == "edit") {
    GetReport({ id: route.query.id }).then((res) => {
      if (res.code === 4201) {
        state.form = res.data;
      }
    });
  }
};
// 类型下拉查询
const GetType = async () => {
  const res = await GetDictionaryTree({ dicModel: "hangyebaogaoleixing" });
  if (res.code == 4201) {
    state.type = res.data;
  }
};
// 图片上传
const successImg = (res) => {
  state.form.wenjian.push(res);
};
// 上传文件删除
const removeImg = (index) => {
  state.form.wenjian.splice(index, 1);
};
// 富文本编辑器
const onVal = (val) => {
  state.form.baogaoneirong = val;
};
// 提交表单
const submitForm = async (formEl) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      if (route.query.type == "add") {
        AddReport(state.form).then((res) => {
          if (res.code == 4202) {
            baseMsg("新增行业报告成功");
            router.go(-1);
          }
        });
      }
      if (route.query.type == "edit") {
        EditReport(state.form).then((res) => {
          if (res.code == 4202) {
            baseMsg("编辑行业报告成功");
            router.go(-1);
          }
        });
      }
    }
  });
};
GetType();
GetInfo();
</script>
<style lang="scss" scoped>
.content {
  border: none !important;
}
.project-info {
  padding: 30px 100px;
  font-size: 20px;
  color: black;
  display: flex;
}
.el-form-item {
  display: flex;
  align-items: center;
  &.vue-editor {
    align-items: baseline;
  }
}
</style>
<style lang="scss">
.form-left-right {
  .el-form-item .el-form-item__label {
    min-width: 95px;
    margin-bottom: 0 !important;
  }
}
.button-group {
  .el-form-item__content {
    width: 100% !important;
    justify-content: center;
  }
}
</style>
